<?php
//zengke
class Url {
	private $get;
	private $path;
	
	public function __construct() {
		$this->get = $this->daddslashes($_GET, 1, TRUE);
		$this->path = isset($_SERVER['SCRIPT_URL']) ? $_SERVER['SCRIPT_URL'] : '/';
	}
	
	public function daddslashes($string, $force = 0, $strip = FALSE) {
		if(get_magic_quotes_gpc() || $force) {
			if(is_array($string)) {
				foreach($string as $key => $val) {
					$string[$key] = $this->daddslashes($val, $force);
				}
			} else {
				$string = addslashes($strip ? stripslashes($string) : $string);
			}
		}
		return $string;
	}

	private function stripallslashes($value) {
		if (!is_array($value)) {
			return trim(stripslashes($value)); // 去掉反斜线 
		} else {
			$vals = array();
			foreach ($value as $key => $val) {
				$vals[$key] = $this->stripallslashes($val); 
			}
			return $vals;
		}
	}

	public function fixGbEncoding() {
		$this->fixArrayEncoding($_GET);
		$this->fixArrayEncoding($_POST);
		$this->get = $this->stripallslashes($_GET);
	}

	private function fixArrayEncoding(&$array) {
		foreach($array as $key => $val) {
			$encode_key = mb_detect_encoding($key, array('UTF-8', 'EUC-CN'));
			$encode_val = mb_detect_encoding($val, array('UTF-8', 'EUC-CN'));
			if($encode_key != 'UTF-8' || $encode_val != 'UTF-8' ) {
				$_tmp = array();
				foreach ($array as $_key => $_val) {
					$_key = mb_convert_encoding($_key, 'UTF-8', 'EUC-CN');
					$_val = mb_convert_encoding($_val, 'UTF-8', 'EUC-CN');
					$_tmp[$_key] = $_val;
				}
				$array = $_tmp;
				return;
			}
		}
	}

	public function setPath($value) {
		$this->path = $value;
		return $this;
	}

	public function set($name, $value) {
		$value = trim(urldecode($value));
		$this->get[$name] = $value;
		return $this;
	}

	public static function getRefer() {
		return isset($_SERVER['HTTP_REFERER']) ? trim($_SERVER['HTTP_REFERER']) : '';
	}

	public static function getUserAgent() {
		return isset($_SERVER['HTTP_USER_AGENT']) ? trim($_SERVER['HTTP_USER_AGENT']) : '';
	}

	public function delete($name) {
		if (isset($this->get[$name])) unset($this->get[$name]);
		return $this;
	}

	public function get($name = null, $default = null) {
		if($name == null) return $this->get;
		return isset($this->get[$name]) ? $this->get[$name]: $default;
	}

	public function post($name = null, $default = null) {
		if($name == null) return $this->daddslashes($_POST, 1, TRUE);
		return isset($_POST[$name]) ? $this->daddslashes($_POST, 1, TRUE) : $default;
	}

	public function __toString() {
		$url = $this->path;
		
		if (count($this->get) > 0) {
			ksort($this->get);
			$url .= '?' .http_build_query($this->get);
		}
		return $url;
	}

	public function switchValues($key1, $key2) {
		$minSal = $this->get($key1);
		$maxSal = $this->get($key2);
		if (strlen($minSal) > 0 && strlen($maxSal) > 0 && $minSal > $maxSal && ($maxSal != '*')) {
			$this->set($key1, $maxSal);
			$this->set($key2, $minSal);
		}
	}

	public static function redirect($uri = '', $method = '302') {
		$uri = str_replace("\n", '', $uri);

		$codes = array(
				'300' => 'Multiple Choices',
				'301' => 'Moved Permanently',
				'302' => 'Found',
				'303' => 'See Other',
				'304' => 'Not Modified',
				'305' => 'Use Proxy',
				'307' => 'Temporary Redirect'
		);
		$method = isset($codes[$method]) ? $method : '302';

		header('HTTP/1.1 '.$method.' '.$codes[$method]);
		header('Location: '.$uri);
		exit('<a href="'.$uri.'">'.$uri.'</a>'); // Last resort, exit and display the URL
	}

	public static function getFullUrl() {
		if(PHP_SAPI == 'cli') {
			$args = '';
			foreach($_SERVER['argv'] as $a) {
				$args .= $a . ' ';
			}
			return $args;
		} else {
			return 	'http://' . $_SERVER['HTTP_HOST'] . (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '');
		}
	}

	public static function innerKeywords() {
		$refer = self::getRefer();
		if (preg_match('/[?&]query=([^&]+)/', $refer, $matches)) { // Inner
			return urldecode($matches[1]);
		}
		return '';
	}

	public static function seoKeywords() {
		$refer = self::getRefer();
		if (preg_match("/baidu.*[?&]wd=([^&]+)/", $refer, $matches)) { //百度
			return mb_convert_encoding(urldecode($matches[1]), 'utf8', 'gbk');
		} else if (preg_match("/google.*[?&]q=([^&]+)/", $refer, $matches)) { // Google
			return urldecode($matches[1]);
		} else if (preg_match("/sogou.*[?&]query=([^&]+)/", $refer, $matches)) { // Sogou
			return mb_convert_encoding(urldecode($matches[1]), 'utf8', 'gbk');
		}
		return '';
	}
}
?>